// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackupViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="BackupViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Management.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows;
    using System.Windows.Input;
    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Common.Services;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Win32;

    /// <summary>
    /// Defines the <seealso cref="BackupViewModel"/> type.
    /// </summary>
    public class BackupViewModel : ViewModelBase
    {
        /// <summary>
        /// Member variable _dataManager
        /// </summary>
        private readonly IDataManager _dataManager;

        /// <summary>
        /// Member variable _session
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Member variable _exportBrowseCommand
        /// </summary>
        private ICommand _exportBrowseCommand;

        /// <summary>
        /// Member variable _importBrowseCommand
        /// </summary>
        private ICommand _importBrowseCommand;

        /// <summary>
        /// Member variable _exportCommand
        /// </summary>
        private DelegateCommand _exportCommand;

        /// <summary>
        /// Member variable _exportCommand
        /// </summary>
        private DelegateCommand _importCommand;

        /// <summary>
        /// Member variable _importFilePath
        /// </summary>
        private string _importFilePath;

        /// <summary>
        /// Member variable _importPassword
        /// </summary>
        private string _importPassword;

        /// <summary>
        /// Member variable _exportFilePath
        /// </summary>
        private string _exportFilePath;

        /// <summary>
        /// Member variable _exportPassword
        /// </summary>
        private string _exportPassword;

        /// <summary>
        /// Initializes a new instance of the <see cref="BackupViewModel"/> class.
        /// </summary>
        public BackupViewModel()
            : this(ServiceLocator.Current.GetInstance<IDataManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BackupViewModel"/> class.
        /// </summary>
        /// <param name="dataManager">The data manager.</param>
        /// <param name="session">The session.</param>
        public BackupViewModel(IDataManager dataManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(dataManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _dataManager = dataManager;
            _session = session;
        }

        /// <summary>
        /// Gets the browse command.
        /// </summary>
        public ICommand ExportBrowseCommand
        {
            get
            {
                if (_exportBrowseCommand == null)
                {
                    _exportBrowseCommand = new DelegateCommand(OnExportBrowse);
                }

                return _exportBrowseCommand;
            }
        }

        /// <summary>
        /// Gets the browse command.
        /// </summary>
        public ICommand ImportBrowseCommand
        {
            get
            {
                if (_importBrowseCommand == null)
                {
                    _importBrowseCommand = new DelegateCommand(OnImportBrowse);
                }

                return _importBrowseCommand;
            }
        }

        /// <summary>
        /// Gets the export command.
        /// </summary>
        public ICommand ExportCommand
        {
            get
            {
                if (_exportCommand == null)
                {
                    _exportCommand = new DelegateCommand(OnExport, CanExport);
                }

                return _exportCommand;
            }
        }

        /// <summary>
        /// Gets the export command.
        /// </summary>
        public ICommand ImportCommand
        {
            get
            {
                if (_importCommand == null)
                {
                    _importCommand = new DelegateCommand(OnImport, CanImport);
                }

                return _importCommand;
            }
        }

        /// <summary>
        /// Gets or sets the file path.
        /// </summary>
        /// <value>
        /// The file path.
        /// </value>
        public string ExportFilePath
        {
            get
            {
                return _exportFilePath;
            }

            set
            {
                RaisePropertyChangingEvent("ExportFilePath");
                _exportFilePath = value;
                RaisePropertyChangedEvent("ExportFilePath");
            }
        }

        /// <summary>
        /// Gets or sets the pasword.
        /// </summary>
        /// <value>
        /// The pasword.
        /// </value>
        public string ExportPassword
        {
            get
            {
                return _exportPassword;
            }

            set
            {
                RaisePropertyChangingEvent("ExportPassword");
                _exportPassword = value;
                RaisePropertyChangedEvent("ExportPassword");
            }
        }

        /// <summary>
        /// Gets or sets the file path.
        /// </summary>
        /// <value>
        /// The file path.
        /// </value>
        public string ImportFilePath
        {
            get
            {
                return _importFilePath;
            }

            set
            {
                RaisePropertyChangingEvent("ImportFilePath");
                _importFilePath = value;
                RaisePropertyChangedEvent("ImportFilePath");
            }
        }

        /// <summary>
        /// Gets or sets the pasword.
        /// </summary>
        /// <value>
        /// The pasword.
        /// </value>
        public string ImportPassword
        {
            get
            {
                return _importPassword;
            }

            set
            {
                RaisePropertyChangingEvent("ImportPassword");
                _importPassword = value;
                RaisePropertyChangedEvent("ImportPassword");
            }
        }

        /// <summary>
        /// Called when browse button clicked
        /// </summary>
        public void OnExportBrowse()
        {
            var dlg = new SaveFileDialog { DefaultExt = "bak", CheckPathExists = true };
            if (dlg.ShowDialog(Application.Current.MainWindow).Value)
            {
                ExportFilePath = dlg.FileName;
            }
        }

        /// <summary>
        /// Called when browse button clicked
        /// </summary>
        public void OnImportBrowse()
        {
            var dlg = new OpenFileDialog { DefaultExt = "bak", CheckPathExists = true, CheckFileExists = true };
            if (dlg.ShowDialog(Application.Current.MainWindow).Value)
            {
                ImportFilePath = dlg.FileName;
            }
        }

        /// <summary>
        /// Determines whether this instance can export.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance can export; otherwise, <c>false</c>.
        /// </returns>
        public bool CanExport()
        {
            return !string.IsNullOrWhiteSpace(ExportFilePath);
        }

        /// <summary>
        /// Called when [export].
        /// </summary>
        public void OnExport()
        {
            _dataManager.ExportData(ExportFilePath, _session.LoggedUser, ExportPassword);
        }

        /// <summary>
        /// Determines whether this instance can export.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance can export; otherwise, <c>false</c>.
        /// </returns>
        public bool CanImport()
        {
            return !string.IsNullOrWhiteSpace(ImportFilePath);
        }

        /// <summary>
        /// Called when [export].
        /// </summary>
        public void OnImport()
        {
            _dataManager.ImportData(ImportFilePath, _session.LoggedUser, ImportPassword);
        }

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the changed property.</param>
        public override void RaisePropertyChangedEvent(string propertyName)
        {
            base.RaisePropertyChangedEvent(propertyName);
            if (_importCommand != null)
            {
                _importCommand.RaiseCanExecuteChanged();
            }

            if (_exportCommand != null)
            {
                _exportCommand.RaiseCanExecuteChanged();
            }
        }
    }
}